@extends('admin.layout.lst')

@section('search')

    <div class="layui-inline">
        <label class="layui-form-label">角色名</label>
        <div class="layui-input-block">
            <input type="text" name="name" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>

@endsection

@section('js')

    <script>
        lay.use(['index', 'table'], function(){

            var $ = layui.$,form = layui.form,table = layui.table;

            var urlAdd  = "{{url('guard/rbac/role/add')}}";
            var urlDel  = "{{url('guard/rbac/role/del')}}";
            var urlEdit = "{{url('guard/rbac/role/edit')}}";

            table.render({
                elem: '#LAY-record'
                ,url: "{{url()->current()}}"
                ,cols: [[
                    {field: 'id', title: 'ID', sort:true ,width:100},
                    {field: 'name', title: '角色'},
                    {field: 'beizhu', title: '备注'},
                    {field: 'created_at', title: '创建时间'},
                    {toolbar: '#table-operation', title: '操作',align:'center'},
                ]]
                ,parseData: function(res){ //res 即为原始返回的数据
                    return {
                        "code": 0, //解析接口状态
                        "msg": res.msg, //解析提示文本
                        "count": res.data.total, //解析数据长度
                        "data": res.data.records //解析数据列表
                    };
                }
                ,page: true
                ,limit: 30
                ,toolbar: '#toolbarDemo'
                ,defaultToolbar: []
                ,height: 'full-120'
                ,text: {
                    none: '暂无相关数据' //默认：无数据。
                }
            });

            //表格顶部工具
            table.on('toolbar(LAY-record)',function (obj){

                if(obj.event==='add'){
                    layer.open({
                        type: 2
                        ,title: '添加角色'
                        ,content: urlAdd
                        ,maxmin: true
                        ,area: [w+'px', h+'px']
                        ,btn: ['确定', '取消']
                        ,yes: function(index, layero){
                            var iframeWindow = window['layui-layer-iframe'+ index]
                                ,submitID = 'LAY-submit'
                                ,submit = layero.find('iframe').contents().find('#'+ submitID);

                            //监听提交
                            iframeWindow.layui.form.on('submit('+ submitID +')', function(data){

                                var field = data.field; //获取提交的字段
                                $.post(urlAdd,field,function (res){
                                    if(res.status){
                                        Dialog.msgSuccessRight(res.msg);
                                        table.reload('LAY-record'); //数据刷新
                                        layer.close(index); //关闭弹层
                                    }else{
                                        Dialog.msgErrorFalse(res.msg);
                                    }
                                },'json');
                            });
                            submit.trigger('click');
                        }
                    });
                }else if(obj.event==='update'){
                    var checkStatus = table.checkStatus(obj.config.id);
                    //console.log(checkStatus);
                    //console.log(obj);
                }else if(obj.event==='delete'){

                }else{

                }

            });

            //行工具
            table.on('tool(LAY-record)', function(obj){
                var data = obj.data;
                if(obj.event === 'del'){
                    layer.confirm('确定要删除吗？', function(index){
                        obj.del();
                        var url = urlDel+"?id="+data.id;
                        $.getJSON(url,function (res){
                            if(res.status){
                                Dialog.msgSuccessRight(res.msg);
                            }else{
                                Dialog.msgErrorFalse(res.msg);
                                table.reload('LAY-record');
                            }
                        });
                    });
                } else if(obj.event === 'edit'){
                    var tr = $(obj.tr);

                    layer.open({
                        type: 2
                        ,title: '编辑角色'
                        ,content: urlEdit+'?id='+data.id
                        ,maxmin: true
                        ,area: [w+'px', h+'px']
                        ,btn: ['确定', '取消']
                        ,yes: function(index, layero){
                            var iframeWindow = window['layui-layer-iframe'+ index]
                                ,submitID = 'LAY-submit'
                                ,submit = layero.find('iframe').contents().find('#'+ submitID);

                            //监听提交
                            iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                                var field = data.field; //获取提交的字段
                                $.post(urlAdd,field,function (res){
                                    if(res.status){
                                        Dialog.msgSuccessRight(res.msg);
                                        table.reload('LAY-record'); //数据刷新
                                        layer.close(index); //关闭弹层
                                    }else{
                                        Dialog.msgErrorFalse(res.msg);
                                    }
                                },'json');
                                layer.close(index); //关闭弹层
                            });
                            submit.trigger('click');
                        }
                        /*,success: function(layero, index){

                        }*/
                    });
                }
            });

            //监听搜索
            form.on('submit(front-search)', function(data){
                var field = data.field;
                table.reload('LAY-record', {where: field});
            });

        });
    </script>

@endsection
